package main

import (
	"fmt"

	"github.com/mitchellh/mapstructure"
)

type Foo struct {
	Name         string `json:"name,omitempty"`
	Age          int    `json:"age,omitempty"`
	ThreeMeature []int  `json:"three_meature,omitempty"`
}

func main() {
	var data = map[string]string{
		"name":         "Jane",
		"age":          "10",
		"ThreeMeature": "15",
	}

	var foo Foo

	decode(data, defaultDecoderConfig(&foo))
	fmt.Println("------", foo)
}

func decode(input interface{}, config *mapstructure.DecoderConfig) error {
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return err
	}
	return decoder.Decode(input)
}

// defaultDecoderConfig returns default mapsstructure.DecoderConfig with suppot
// of time.Duration values & string slices
func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig {
	c := &mapstructure.DecoderConfig{
		Metadata:         nil,
		Result:           output,
		WeaklyTypedInput: true,
		DecodeHook: mapstructure.ComposeDecodeHookFunc(
			mapstructure.StringToTimeDurationHookFunc(),
			mapstructure.StringToSliceHookFunc(","),
		),
	}
	return c
}
